package com.cswnode.Service.impl;

import com.cswnode.Dao.LoginTicketMapper;
import com.cswnode.Dao.UserMapper;
import com.cswnode.Entity.LoginTicket;
import com.cswnode.Entity.User;
import com.cswnode.Enums.ResponseEnum;
import com.cswnode.Service.LoginService;
import com.cswnode.Util.CommunityUtil;
import com.cswnode.Util.Constant;
import com.cswnode.Util.RedisKeyUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author csw
 * @create 2020/9/16 17:08
 */
@Service
public class LoginServiceImpl implements LoginService {

//    @Autowired
//    private LoginTicketMapper loginTicketMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public Map<String, Object> Login(String username, String password, int expired) {
        Map<String,Object> map = new HashMap<>();
        if (StringUtils.isBlank(username)){
            map.put("usernameMsg","用户名不能为空");
            return map;
        }
        if (StringUtils.isBlank(password)){
            map.put("passwordMsg","密码不能为空");
            return map;
        }
        User user = userMapper.selectByName(username);
        //验证用户
        if (user == null){
            map.put("usernameMsg","用户名不存在！");
            return map;
        }
        //验证激活状态
        if (user.getStatus() == Constant.USER_NOT_ACTIVE){
            map.put("usernameMsg","该账户未激活！");
            return map;
        }
        //验证密码
        String MD5Password = CommunityUtil.MD5(password+user.getSalt());
        if (!user.getPassword().equals(MD5Password)){
            map.put("passwordMsg","密码错误，请重新登陆");
            return map;
        }
        //生成用户凭证
        LoginTicket loginTicket = new LoginTicket();
        loginTicket.setUserId(user.getId());
        loginTicket.setStatus(Constant.LOGINTICKET_STATUS_ACTIVATION);
        loginTicket.setTicket(CommunityUtil.getUUID());
        loginTicket.setExpired(new Date(System.currentTimeMillis() + Long.valueOf(expired) * 1000));

        //loginTicketMapper.insertLoginTicket(loginTicket);
        String redisKey = RedisKeyUtil.getTicket(loginTicket.getTicket());
        redisTemplate.opsForValue().set(redisKey,loginTicket);

        map.put("ticket",loginTicket.getTicket());
        return map;
    }

    @Override
    public void logout(String ticket) {
        //loginTicketMapper.updateBystatus(ticket,Constant.LOGINTICKET_STATUS_NO_ACTIVATION);
        String ticket1 = RedisKeyUtil.getTicket(ticket);
        LoginTicket loginTicket = (LoginTicket) redisTemplate.opsForValue().get(ticket1);
        loginTicket.setStatus(1);
        redisTemplate.opsForValue().set(ticket1,loginTicket);

    }

    @Override
    public LoginTicket findLoginTicket(String ticket) {
        //return loginTicketMapper.selectByTicket(ticket);
        String redisKey = RedisKeyUtil.getTicket(ticket);
        return (LoginTicket) redisTemplate.opsForValue().get(redisKey);
    }



}
